<template>
	<view class="w-1 fl-s-c">
		<view class="w-1" style="height: 16rpx; background: #F5F5F5;"></view>
		<view class="w-1 p-r-30">
			<view @click="$u.route('/pagemaster/pages/Login/ForgetPwd')" class="w-1 relative bb-solid fr-s-c"
				style="padding: 30rpx 0;">
				<text class="c-33 bold fs-28 flex-1" style="width: 120rpx;">修改密码</text>
				<up-icon name="arrow-right"></up-icon>
			</view>
			<view @click="$u.route('/pagemaster/pages/Login/ChangePhone')" class="w-1 relative bb-solid fr-s-c"
				style="padding: 30rpx 0;">
				<text class="c-33 bold fs-28 flex-1" style="width: 120rpx;">更换手机号</text>
				<up-icon name="arrow-right"></up-icon>
			</view>
		</view>
		<view class="w-1" style="height: 16rpx; background: #F5F5F5;"></view>
		<view class="w-1 p-r-30">
			<view @click="$u.route('/pagemaster/pages/User/AboutUs')" class="w-1 relative bb-solid fr-s-c"
				style="padding: 30rpx 0;">
				<text class="c-33 bold fs-28 flex-1" style="width: 120rpx;">关于我们</text>
				<up-icon name="arrow-right"></up-icon>
			</view>
			<view @click="$u.route('/pagemaster/pages/RichText/RichText?type=3')" class="w-1 relative bb-solid fr-s-c"
				style="padding: 30rpx 0;">
				<text class="c-33 bold fs-28 flex-1" style="width: 120rpx;">用户协议</text>
				<up-icon name="arrow-right"></up-icon>
			</view>
			<view @click="$u.route('/pagemaster/pages/RichText/RichText?type=2')" class="w-1 relative bb-solid fr-s-c"
				style="padding: 30rpx 0;">
				<text class="c-33 bold fs-28 flex-1" style="width: 120rpx;">隐私政策</text>
				<up-icon name="arrow-right"></up-icon>
			</view>
			<view @click="clean" class="w-1 relative bb-solid fr-s-c"
				style="padding: 30rpx 0;">
				<text class="c-33 bold fs-28 flex-1" style="width: 120rpx;">清除缓存</text>
				<up-icon name="arrow-right"></up-icon>
			</view>
			<view class="w-1 relative bb-solid fr-s-c" style="padding: 30rpx 0;">
				<text class="c-33 bold fs-28 flex-1" style="width: 120rpx;">当前版本</text>
				<text class="c-33 fs-28">V{{version}}</text>
				<up-icon name="arrow-right"></up-icon>
			</view>
			<view @click="zx" class="w-1 relative bb-solid fr-s-c" style="padding: 30rpx 0;">
				<text class="c-33 bold fs-28 flex-1" style="width: 120rpx;">注销账户</text>
				<up-icon name="arrow-right"></up-icon>
			</view>
		</view>

		<view class="iphonex">
			<view class="p-30">
				<view class="big-btn fr-c m-t-25" style="background: #ff5c55; " @click="logout">退出当前账号</view>
			</view>
		</view>
	</view>
</template>


<script>
	export default {
		data() {
			return {
				app: '',
				value: true,
				version: uni.getSystemInfoSync().version || '1.0.0',
				progress:0,
				phone:'',
				update:0,
				fileSizeString:'0B'
			}
		},
		onLoad(o) {
			this.update = o.update || 0;
			this.getinfo()
			this.formatSize()
		},
		watch:{
			update(val){
				console.log('watcwatchwatchwatchwatchwatchh',val)
				if(val==1){
					this.subupdate()
				}
			},
		},
		methods: {
			logout() {
				uni.clearStorageSync()
				uni.reLaunch({
					url: '/pages/Login/Login'
				})
			},
			zx() {
				uni.navigateTo({
					url:'/pagemaster/pages/User/LogOff1'
				})
				// uni.showModal({
				// 	title: '提示',
				// 	content: '确认注销账号吗？',
				// 	success: (res) => {
				// 		if (res.confirm) {
				// 			uni.$post({
				// 				url: '/api/User/Cancel_user'
				// 			}).then(res => {
				// 				uni.clearStorageSync()
				// 				uni.reLaunch({
				// 					url: '/pagemaster/pages/Login/Login'
				// 				})
				// 			})
				// 		}
				// 	}
				// })
			},
			clean() {
				let that = this
				uni.showModal({
					title: '清除缓存',
					content: '您确定要清除缓存吗？',
					success: function(res) {
						if (res.confirm) {
							console.log('用户点击确定');
							that.clearCache()
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			// 清理缓存
			clearCache() {
				// #ifdef APP-PLUS
				let that = this;
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
							if (entry.isDirectory) {
								entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录  
									uni.showToast({
										title: '缓存清理完成',
										duration: 2000
									});
									that.formatSize(); // 重新计算缓存  
								}, function(e) {
									console.log(e.message)
								});
							} else {
								entry.remove();
							}
						}, function(e) {
							console.log('文件路径读取失败')
						});
					}
				} else { // ios  
					plus.cache.clear(function() {
						uni.showToast({
							title: '缓存清理完成',
							duration: 2000
						});
						that.formatSize();
					});
				}
				// #endif
			
			},
			// 获取缓存
			formatSize() {
				// #ifdef APP-PLUS
				let that = this;
				plus.cache.calculate(function(size) {
					let sizeCache = parseInt(size);
					if (sizeCache == 0) {
						that.fileSizeString = "0B";
					} else if (sizeCache < 1024) {
						that.fileSizeString = sizeCache + "B";
					} else if (sizeCache < 1048576) {
						that.fileSizeString = (sizeCache / 1024).toFixed(2) + "KB";
					} else if (sizeCache < 1073741824) {
						that.fileSizeString = (sizeCache / 1048576).toFixed(2) + "MB";
					} else {
						that.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "GB";
					}
				});
				// #endif
			},
			subupdate(){
				uni.$get({
					url: '/versionappment'
				}).then(res=>{
					plus.runtime.getProperty(plus.runtime.appid, (info) => {
						if(plus.os.name == "Android"){
							this.showUpdata(res.data.android_targetFile)
						}else{
							this.showUpdata(res.data.ios_targetFile)
						}
					})
				})
			},
			copy(data) {
				uni.setClipboardData({
					data
				})
			},
			// app获取版本号
			getinfo() {
				// #ifdef APP-PLUS
				// 获取本地应用资源版本号
				plus.runtime.getProperty(plus.runtime.appid, (info) => {
					this.version = info.version;
				})
				// #endif
			},
			check() {
				// #ifdef APP-PLUS
				uni.$get({
					url: '/versionappment'
				}).then(res => {
					console.log('看看版本',res)
					let v_num = this.version.split('.').join('') * 1;
					let n_num = '';
					if(plus.os.name == "Android"){
						n_num = res.data.android_varsn.split('.').join('') * 1;
					}else{
						n_num = res.data.ios_varsn.split('.').join('') * 1;
					}
					if (n_num > v_num) {
						uni.showModal({
							title: '提示',
							content: '检测到新版本，是否下载更新？',
							success: (ret) => {
								if (ret.confirm) {
									plus.runtime.getProperty(plus.runtime.appid, (info) => {
										if(plus.os.name == "Android"){
											this.showUpdata(res.data.android_targetFile)
										}else{
											this.showUpdata(res.data.ios_targetFile)
										}
									})
								}
							}
						})
					} else {
						uni.showModal({
							content: '已是最新版本!',
							showCancel: false
						})
					}
				})
				// #endif
				// #ifdef H5
				uni.showModal({
					content: '已是最新版本!',
					showCancel: false
				})
				// #endif
			},
			showUpdata(origin_url) {
				// #ifdef APP-PLUS
				if (plus.os.name == "Android") {
					// uni.showLoading({
					// 	title: '更新中...'+this.progress,
					// 	mask: true
					// })
					const downloadTask = uni.downloadFile({ //执行下载
						url: origin_url, //下载地址
						success: downloadResult => { //下载成功
							uni.hideLoading();
							if (downloadResult.statusCode == 200) {
								// #ifdef APP-PLUS
								uni.showModal({
									title:'更新',
									content:'app已下载完成，是否立即更新？',
									success: (res) => {
										if(res.confirm){
											plus.runtime.install(downloadResult.tempFilePath); // 自动安装apk文件
											plus.runtime.quit();
										}else{
											this.progress = 0;
										}
									}
								})
								// #endif
							}
						}
					});
					// 下载进度
					downloadTask.onProgressUpdate(res => {
						console.log('下载进度' + res.progress);
						this.progress = res.progress;
						this.$forceUpdate()
						uni.showLoading({
							title: res.progress,
							mask: true
						})
						if (res.progress == 100) {
							uni.hideLoading()
						}
					});
				} else {
					//ios
					plus.runtime.openURL(origin_url) //url:app对应的apple store地址
				}
				// #endif
			},
			change(val) {
				console.log(val)
			},
			toRichText(n) {
				uni.navigateTo({
					url: '/pagemaster/pages/RichText/RichText?type=' + n
				})
			},
		}
	}
</script>

<script setup>
	import {
		reactive
	} from 'vue';
</script>

<style>
	page {
		background: #ffffff;
	}
</style>